package cn.xfdzx.android.apps.shop.fragment.car;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ConfirmOrderActivity;
import cn.xfdzx.android.apps.shop.activity.XFDConfirmOrderActivity;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.CarRetailAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ShopCarListContract;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.PostCarNumMessage;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：购物车社区零售
 */
public class CarCommunityRetailFragment extends BaseMvpFragment<ShopCarListPresenter> implements ShopCarListContract.View {

    RecyclerView rvCarRetail;

    private List<ShopCarListBeen.DataBean.ValidBean> mList;
    private CarRetailAdapter carRetailAdapter;
    private LinearLayout llCarEmpty;//购物车为空

    @Override
    protected void initView(View view) {
        mPresenter = new ShopCarListPresenter();
        mPresenter.attachView(this);
        mList = new ArrayList<>();
        rvCarRetail = view.findViewById(R.id.rv_retail);
        llCarEmpty = view.findViewById(R.id.ll_car_empty);

        EventBus.getDefault().register(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCarRetail.setLayoutManager(linearLayoutManager);
        rvCarRetail.setItemAnimator(new DefaultItemAnimator());
        carRetailAdapter = new CarRetailAdapter(getContext(), mList, mPresenter);
        carRetailAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
        rvCarRetail.setAdapter(carRetailAdapter);

//        mPresenter.initData("0");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_car_community_retail;
    }

    @Override
    public void showLoading() {
        if (TYPE_IS_RETAIL == 0) {
            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
                ProgressDialog.getInstance().show(getActivity());
            }
        }
    }


    @Override
    public void hideLoading() {
        if (TYPE_IS_RETAIL == 0) {

                ProgressDialog.getInstance().dismiss();

        }
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(CarInitDataMessage messageEvent) {
        if (messageEvent.getMessage() == 0) {

            mPresenter.initData("0");
        }
    }

    @Override
    public void onInitSuccess(ShopCarListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {

            mList.clear();
            mList.addAll(bean.getData().getValid());
            for (ShopCarListBeen.DataBean.ValidBean validBean : bean.getData().getInvalid()) {
                validBean.setIs_select(true);
                for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : validBean.getGoods()) {
                    goodsBeanX.setIs_select(true);
                }
            }
            if (bean.getData().getInvalid().size() != 0) {
                for (int i = 1; i < bean.getData().getInvalid().size(); i++) {
                    bean.getData().getInvalid().get(0).getGoods().addAll(bean.getData().getInvalid().get(i).getGoods());
                }

                bean.getData().getInvalid().get(0).setIs_shixiao(true);
                mList.add(bean.getData().getInvalid().get(0));
            }
            carRetailAdapter.notifyDataSetChanged();
            if (mList.size()==0){
                llCarEmpty.setVisibility(View.VISIBLE);
            }else {
                llCarEmpty.setVisibility(View.GONE);
            }
        } else {

        }
    }

    @Override
    public void onUpdateCarNumSuccess(BaseObjectBean bean, int number, int one_position, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.get(one_position).getGoods().get(position).setCart_num(number);
            carRetailAdapter.notifyDataSetChanged();

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(getActivity(), "login").remove("head_url");
                UtilsData.getPreference(getActivity(), "login").remove("nickname");
                UtilsData.getPreference(getActivity(), "login").remove("is_store");
                UtilsData.getPreference(getActivity(), "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRemoveGoodSuccess(BaseObjectBean bean, int one_position, int position,boolean isAll) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            //刷新购物车数据
            EventBus.getDefault().post(new PostCarNumMessage());
            if (mList.get(one_position).getGoods().size() == 1||isAll) {
                mList.remove(one_position);
                if (mList.size()==0){
                    llCarEmpty.setVisibility(View.VISIBLE);
                }else {
                    llCarEmpty.setVisibility(View.GONE);
                }
            } else {
                mList.get(one_position).getGoods().remove(position);
            }
            carRetailAdapter.notifyDataSetChanged();
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(getActivity(), "login").remove("head_url");
                UtilsData.getPreference(getActivity(), "login").remove("nickname");
                UtilsData.getPreference(getActivity(), "login").remove("is_store");
                UtilsData.getPreference(getActivity(), "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onConfirmSuccess(ConfirmGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(getActivity(), ConfirmOrderActivity.class);
            intent.putExtra("confirm", bean.getData());
            startActivity(intent);
        }else if (bean.getStatus()==2000) {
            Toast.makeText(getActivity(), "此商铺已打烊", Toast.LENGTH_SHORT).show();
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(getActivity(), "login").remove("head_url");
                UtilsData.getPreference(getActivity(), "login").remove("nickname");
                UtilsData.getPreference(getActivity(), "login").remove("is_store");
                UtilsData.getPreference(getActivity(), "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void dialog_num(int one_position, int two_position) {
       //不是这里用的
    }

    public static CarCommunityRetailFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
//        bundle.putString("item", "UserPostFragment");
//        bundle.putLong(Constants.UID, userUid);
        CarCommunityRetailFragment fragment = new CarCommunityRetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {

        mList.clear();
        mList = null;
        super.onDestroy();
    }
}
